import { getGoodBomChildListByGoodId } from '@/api/goodsInfo'
import { ref } from 'vue'

export default function () {
  const getGoodBomChildListLoading = ref(false)
  const getGoodBomChildListParam = ref({
    limit: 100,
    page: 1,
  })
  const goodBomChildListTotal = ref(0)
  const goodBomChildList = ref<Array<GoodAPIResponse.GoodBomChildListItem>>([])

  const resetGetSparePartListParam = () => {
    getGoodBomChildListParam.value = {
      limit: 100,
      page: 1,
    }
  }

  const getGoodBomChildList = async (goodId: string) => {
    try {
      getGoodBomChildListLoading.value = true
      const res = await getGoodBomChildListByGoodId(goodId, getGoodBomChildListParam.value)
      getGoodBomChildListLoading.value = false
      if (res.data.code === 20000) {
        goodBomChildListTotal.value = Number(res.data.data.total)
        goodBomChildList.value = res.data.data.rows
        return ''
      }

      return res.data.message
    } catch (error) {
      getGoodBomChildListLoading.value = false
    }
  }

  return {
    goodBomChildList,
    goodBomChildListTotal,
    getGoodBomChildListParam,
    getGoodBomChildListLoading,
    getGoodBomChildList,
    resetGetSparePartListParam,
  }
}
